// pages/atsAthleteGaineds/atsAthleteGaineds.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   * xindeAll  心得
   * objectArray 运动员
   * index  选中远动员的下标
   * athleteId  选中远动员的id
   * type  用户类型
   * page 当前页数
   * limit 一次加载个数
   * context 搜索框的值
   */
  data: {
    xindeAll:[],
    objectArray:[],
    index:0,
    athleteId:0,
    type:0,
    page:1,
    limit:5,
    context:"",
  
  },
  bindPickerChange: function (e) {
    
    this.setData({
      index: e.detail.value,
      xindeAll: [],
      athleteId: this.data.objectArray[e.detail.value].athleteId
    })
    this.selectXinde()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    app.login();
    const that=this;
    let user = wx.getStorageSync('user');
    
    //设置进来的用户的类型
    that.setData({
      type: user.data.companyNum
    })
    if (user.data.companyNum == 4){
          that.setData({
          
            athleteId: user.data.filId
          //  athleteId:102
          })
          that.selectXinde()
     } else  {
      wx.request({
        url: app.globalData.url + 'weixinController/selectAthleteAll',

        data: {
          coachId: user.data.filId,
          type: user.data.companyNum
          //coachId:72
        },
        success: ress => {
          console.info(ress.data)
          that.setData({
            objectArray: ress.data,
            athleteId: ress.data[0].athleteId
          })
          that.selectXinde()
        }
      })

    } 
      
  

  },
  showModal:function(e){
    wx.navigateTo({
      url: '../atsAthleteGained/atsAthleteGained?id=' + e.currentTarget.dataset.text,
      success: () => {

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
 
   /**
   * 搜索函数
   */
  
  blur: function (e) {
    this.setData({
      context:e.detail.value,
      xindeAll:[],
      page: 1
    })
    console.info(e)
    this.selectXinde();
  },
  /**
   * 显示更多函数
   */
  next: function (e) {
this.setData({
  page:this.data.page+1
})
    this.selectXinde();
  },
  add:function(e){
    wx.showLoading({
      title: '加载中',
    })
    wx.navigateTo({
      url: '../AddGained/AddGained',
      success: () => {
        wx.hideLoading()
      }
    })
  },
  /**
   * 运动员id查心得
   */
  selectXinde:function(){
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.url +'weixinController/findAllXinDe',

      data: {
        athleteId: this.data.athleteId,
        page: this.data.page,
        limit: this.data.limit,
        gainedTime: this.data.context
      },
      success: ress => {
        wx.hideLoading()
        console.info(ress.data)
        if (ress.data.length==0){
          wx.showToast({
            title: '没有数据了',
            icon: 'none',
            duration: 2000
          })
          
       
          return
        }
        var arr=this.data.xindeAll;
       
        for (var i = 0; i < ress.data.length;i++){
          arr.push(ress.data[i])
        }
        this.setData({
          xindeAll: arr,
         
        })
       // console.info(this.data.xindeAll)

      }
    })
  }
})